<?php
//error_reporting(E_ALL);
//ini_set('display_errors','1');


class Lists extends CI_Controller {

    private $dir = 'EmailMarketing';
    function __construct() {		

        parent::__construct();
         $this->is_login(2);
        $this->load->library('ion_auth');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->library('pagination');
        $this->load->database();
        $this->lang->load('auth');
        $this->lang->load('admin_estate');
        $this->load->helper('language');
        $this->load->model('admin_estate_model');

		$this->load->helper('email');
		$this->load->helper('form');
		$this->load->helper('url');
		$this->load->helper('string');

        $this->load->model('mod_emailmarketing');
		$this->load->model('mod_users');
		$this->load->model('emailgroup_model');
		$this->load->model('codegen_model','',TRUE);
    }

    /**
	 * Loads Email settings page.
	 *
	 * @access	private
	 * @param	nil
	 * @return	void
	 */

	function index(){	
	 $data['message'] = $this->session->flashdata('message');
				$data['dir'] = $this->dir;
			    $this->session->set_userdata('page', 22);		
				$this->load->model('list_model');
				$data['subscribers_lists'] = $this->list_model->get_list_subscribers();						
			 	$this->tmadmin->tmView('email_marketing/list_index', $data);
	}
	
//Imtiaz
 public function is_login($level = 2) {
        if (!$this->ion_auth->logged_in()) {
            //redirect them to the login page
            redirect('login', 'refresh');
        } elseif (!$this->ion_auth->is_admin($level)) {
            $this->load->helper('admin');
            redirect(site_url(), 'refresh');
        }
    }
			
			private function add_subscibers()
			{
				 $this->load->library('form_validation');    
					$this->data['custom_error'] = '';
		
				if ($this->form_validation->run('mw_list_subscriber') == false)
				{
					 $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">'.validation_errors().'</div>' : false);
		
				} else
				{                            
					$data = array(
							'subscriber_uid' => $this->codegen_model->get_random_uid_string(), 
							'list_id' => $this->uri->segment(5),
							'email' => set_value('email'),
							'status' => set_value('status'),
							'date_added' => $this->codegen_model->get_date(),
							'last_updated' => $this->codegen_model->get_date()
					);	
					if ($this->codegen_model->add('mw_list_subscriber',$data) == TRUE)
					{
						$subscriber_id = $this->db->insert_id();																		
						$data = array(
							array(
								'field_id' => '7' , 
								'subscriber_id' => $subscriber_id,
								'value' => set_value('email'),
								'date_added' => $this->codegen_model->get_date(),
								'last_updated' => $this->codegen_model->get_date()
							),
							array(
								'field_id' => '8' , 
								'subscriber_id' => $subscriber_id,
								'value' => $this->input->post('first_name'),
								'date_added' => $this->codegen_model->get_date(),
								'last_updated' => $this->codegen_model->get_date()
							),
							array(
								'field_id' => '9' , 
								'subscriber_id' => $subscriber_id,
								'value' => $this->input->post('last_name'),
								'date_added' => $this->codegen_model->get_date(),
								'last_updated' => $this->codegen_model->get_date()
							)
							
							);	
							$this->db->insert_batch('mw_list_field_value',$data);	
							redirect('email_marketing/lists');				
					
					}
					else
					{
						echo "error";
					}
				}
				$this->tmadmin->tmView('email_marketing/list_subscriber_add', $data);
			}	
							
	public function _remap($operation,$parameter)
	{			
				if($operation=='subscribers')
				{
					$method=$this->uri->segment(4);
					if($method=='create')
					{
						$this->add_subscibers();
						
					}
				}	
				else if(method_exists($this,$operation))
				{
					$this->$operation($parameter[0]);
				}
				else{
				
				$data['message'] = $this->session->flashdata('message');
				$data['dir'] = $this->dir;
			    $this->session->set_userdata('page', 22);		
				$this->load->model('list_model');
				$data['subscribers_lists'] = $this->list_model->get_list_subscribers();						
			 	$this->tmadmin->tmView('email_marketing/list_index', $data);
				}
		
	}
		
				
		private function create(){        
		
        $this->load->library('form_validation');   
		$this->form_validation->set_error_delimiters('<p class="error">','</p>');
		$this->form_validation->set_message('required', '%s cannot be blank');
		$data['custom_error'] = '';
		
        if ( $this->form_validation->run('mw_list_validation') == false)
        {
             $data['custom_error_un'] = (validation_errors() ? '<div class="form_error">'.validation_errors().'</div>' : false);

        } else
        {                
		
		             
            $data_list = array(   
				   'list_uid' => $this->codegen_model->get_random_uid_string(), 
				   'user_id' => $this->session->userdata('user_id'),
					'name' => set_value('name'),
					'description' => set_value('description'),					
					'opt_in' => set_value('opt_in'),
					'opt_out' => set_value('opt_out'),
					'date_added' => $this->codegen_model->get_date(),
					'last_updated' => $this->codegen_model->get_date(),
					
            );
			
			
			
			
           
			if ($this->codegen_model->add('mw_list',$data_list) == TRUE)
			{
				$data['custom_error'] = '<div class="form_error"><p>Working.</p></div>';
				$list_id = $this->db->insert_id();
				$data_list_default = array(
	     			'list_id' =>$list_id ,   
                    'from_name' => set_value('from_name'),
					'from_email' => set_value('from_email'),
					'reply_to' => set_value('reply_to'),
					'subject' => set_value('subject')
            );
			$data_list_notifications = array(  
					'list_id' =>$list_id ,				            
					'subscribe' => set_value('subscribe'),
					'unsubscribe' => set_value('unsubscribe'),				
					'subscribe_to' => set_value('subscribe_to'),
					'unsubscribe_to' => set_value('unsubscribe_to')
            );
			$data_list_company = array(
					'list_id' =>$list_id , 
                    'country_id' => $this->input->post('country_id'),
					'zone_id' => $this->input->post('zone_id'),
					'name' => $this->input->post('company_name'),
					'address_1' => $this->input->post('address_1'),
					'address_2' => $this->input->post('address_2'),
					'zone_name' => $this->input->post('zone_name'),
					'city' => $this->input->post('city'),
					'zip_code' => $this->input->post('zip_code'),
					'phone' => $this->input->post('phone'),
					'address_format' => $this->input->post('address_format')
            );
				if($this->codegen_model->add('mw_list_default',$data_list_default) == TRUE)
				{
					if ($this->codegen_model->add('mw_list_customer_notification',$data_list_notifications) == TRUE)					
					{
						if ($this->codegen_model->add('mw_list_company',$data_list_company) == TRUE)
						{  
						$this->session->set_flashdata('message', 'List Created Successfully');
						redirect('email_marketing/lists');
						}
					}
					
				}
				//$this->data['custom_error'] = '<div class="form_ok"><p>Added</p></div>';
			
				
			}
			else
			{
				$data['custom_error'] = '<div class="form_error"><p>An Error Occured.</p></div>';

			}
			
			
			
		}
		$data['countries'] = $this->db->get('mw_country')->result();;
				   
		$this->tmadmin->tmView('email_marketing/list_create', $data);
        //$this->template->load('content', 'mw_list_add', $this->data);
    }	
    
   private function edit(){               
   
   $this->load->library('form_validation');   
		$this->form_validation->set_error_delimiters('<p class="error">','</p>');
		$this->form_validation->set_message('required', '%s cannot be blank');
		$data['custom_error'] = '';
		
        if ( $this->form_validation->run('mw_list_validation') == false)
        {
             $data['custom_error_un'] = (validation_errors() ? '<div class="form_error">'.validation_errors().'</div>' : false);

        } else
        {                
		
            $data_list = array(   
				   'list_uid' => $this->codegen_model->get_random_uid_string(), 
				   'user_id' => $this->session->userdata('user_id'),
					'name' => set_value('name'),
					'description' => set_value('description'),					
					'opt_in' => set_value('opt_in'),
					'opt_out' => set_value('opt_out'),
					'date_added' => $this->codegen_model->get_date(),
					'last_updated' => $this->codegen_model->get_date(),
					
            );
			
			
			
			
           
			if ($this->codegen_model->edit('mw_list',$data_list,'list_id' ,$this->uri->segment(4)) == TRUE)
			{
				$data['custom_error'] = '<div class="form_error"><p>Working.</p></div>';				
				$data_list_default = array(	     		
                    'from_name' => set_value('from_name'),
					'from_email' => set_value('from_email'),
					'reply_to' => set_value('reply_to'),
					'subject' => set_value('subject')
            );
			$data_list_notifications = array(  					            
					'subscribe' => set_value('subscribe'),
					'unsubscribe' => set_value('unsubscribe'),				
					'subscribe_to' => set_value('subscribe_to'),
					'unsubscribe_to' => set_value('unsubscribe_to')
            );
			$data_list_company = array(					
                    'country_id' => $this->input->post('country_id'),
					'zone_id' => $this->input->post('zone_id'),
					'name' => $this->input->post('company_name'),
					'address_1' => $this->input->post('address_1'),
					'address_2' => $this->input->post('address_2'),
					'zone_name' => $this->input->post('zone_name'),
					'city' => $this->input->post('city'),
					'zip_code' => $this->input->post('zip_code'),
					'phone' => $this->input->post('phone'),
					'address_format' => $this->input->post('address_format')
            );
				if($this->codegen_model->edit('mw_list_default',$data_list_default,'list_id' ,$this->uri->segment(4)) == TRUE)
				{
					if ($this->codegen_model->edit('mw_list_customer_notification',$data_list_notifications,'list_id' ,$this->uri->segment(4)) == TRUE)					
					{
						if ($this->codegen_model->edit('mw_list_company',$data_list_company,'list_id' ,$this->uri->segment(4)) == TRUE)
						{  
						$this->session->set_flashdata('message', 'List Updated Successfully');            
						redirect('email_marketing/lists');
						}
					}
					
				}
				//$this->data['custom_error'] = '<div class="form_ok"><p>Added</p></div>';
			
				
			}
			else
			{
				$data['custom_error'] = '<div class="form_error"><p>An Error Occured.</p></div>';

			}
			
			
			
		}
		$result = $this->db->get_where('mw_list', array('list_id' => $this->uri->segment(4)));		
		$data['list'] = $result->row_array();		
		$result = $this->db->get_where('mw_list_default', array('list_id' => $this->uri->segment(4)));
		$data['list_default'] = $result->row_array();			
		$result = $this->db->get_where('mw_list_customer_notification', array('list_id' => $this->uri->segment(4)));	
		$data['list_customer_notification'] = $result->row_array();	
		$result = $this->db->get_where('mw_list_company', array('list_id' => $this->uri->segment(4)));	
		$data['list_company'] = $result->row_array();	

		$this->tmadmin->tmView('email_marketing/list_edit', $data);	
		
		
        //$this->template->load('content', 'mw_list_edit', $this->data);
    }
	
    private function delete(){
            $ID =  $this->uri->segment(4);
            $this->codegen_model->delete('mw_list_company','list_id',$ID);
			$this->codegen_model->delete('mw_list_customer_notification','list_id',$ID);
			$this->codegen_model->delete('mw_list_default','list_id',$ID);
			$this->codegen_model->delete('mw_list','list_id',$ID); 
			$this->session->set_flashdata('message', 'List is Deleted');            
            redirect('email_marketing/lists');
    }
	private function overview()
	{
		$data['message'] = $this->session->flashdata('message');
				$data['dir'] = $this->dir;
			    $this->session->set_userdata('page', 22);		
				$this->load->model('list_model');
				$data['subscribers_lists'] = $this->list_model->get_list_subscribers();						
			 	$this->tmadmin->tmView('email_marketing/list_overview', $data);
		
	}
	
	function getcountry($id)
	{
		$datavalues=$this->db->get_where('mw_zone',array('country_id' => $id))->result();
		$this->output->set_output(json_encode($datavalues));	
		
	}
	
	
	function add_subscriber()
	{
		
	}
				
	}

//End  emailmarketing Class



/* End of file emailmarketing.php */ 

/* Location: ./app/controllers/admin/emailmarketing.php */